<!-- eslint-disable vue/no-use-v-if-with-v-for -->
<!-- eslint-disable vue/valid-v-slot -->
<template>
  <div class="main-container">
    <TableBody>
      <template #header>
        <TableHeader ref="tableHeaderRef" :show-filter="false">
          <template #search-content>
            <a-form ref="serchForm" :model="form">
              <a-row :gutter="24">
                <a-col :span="4">
                  <a-form-item field="remarks" hide-label>
                    <a-input v-model="form.remarks" placeholder="请输入订单号" />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="add" hide-label>
                    <a-select v-model="form.add" placeholder="请选择余额增减" allow-clear>
                      <a-option value="0">减少</a-option>
                      <a-option value="1">增加</a-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="type" hide-label>
                    <a-select
                      v-model="form.type"
                      placeholder="请选择账单类型"
                      allow-clear
                      allow-search
                    >
                      <a-option
                        v-for="(item, index) in typeLists"
                        :key="index"
                        :value="item.type"
                        >{{ item.message }}</a-option
                      >
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="addTime" hide-label>
                    <a-date-picker
                      v-model="form.addTime"
                      style="width: 100%"
                      show-time
                      placeholder="请选择开始时间"
                      :time-picker-props="{ defaultValue: '12:00:00' }"
                      format="YYYY-MM-DD HH:mm:ss"
                      allow-clear
                      :shortcuts="[
                        {
                          label: '今日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            ))
                          ),
                        },
                        {
                          label: '昨日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '七日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(6, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '本月',
                          value: () => (
                            (form.addTime = $dayjs('01 00:00:00', 'DD HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '上月',
                          value: () => (
                            (form.addTime = $dayjs(`01 00:00:00`, 'DD HH:mm:ss')
                              .subtract(1, 'month')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs()
                              .subtract(1, 'month')
                              .endOf('month')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                      ]"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="endTime" hide-label>
                    <a-date-picker
                      v-model="form.endTime"
                      style="width: 100%"
                      show-time
                      placeholder="请选择结束时间"
                      :time-picker-props="{ defaultValue: '12:00:00' }"
                      format="YYYY-MM-DD HH:mm:ss"
                      allow-clear
                      :shortcuts="[
                        {
                          label: '今日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            ))
                          ),
                        },
                        {
                          label: '昨日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '七日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(6, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '本月',
                          value: () => (
                            (form.addTime = $dayjs('01 00:00:00', 'DD HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '上月',
                          value: () => (
                            (form.addTime = $dayjs(`01 00:00:00`, 'DD HH:mm:ss')
                              .subtract(1, 'month')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs()
                              .subtract(1, 'month')
                              .endOf('month')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                      ]"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item hide-label>
                    <a-input-group class="width100">
                      <a-input v-model="form.addBalance" placeholder="开始交易金额" />
                      <a-input v-model="form.endBalance" placeholder="结束交易金额" />
                    </a-input-group>
                  </a-form-item>
                </a-col>
                <a-col :span="4" :offset="20">
                  <a-form-item field="btns" content-class=" flex justify-end" hide-label>
                    <a-space :size="15">
                      <a-button type="primary" html-type="submit" @click="doRefresh(1)"
                        ><template #icon>
                          <icon-search />
                        </template>
                        <!-- Use the default slot to avoid extra spaces -->
                        <template #default>搜索</template></a-button
                      >
                      <a-button @click="resetSeach"
                        ><template #icon>
                          <icon-sync theme="outline" size="14" />
                        </template>
                        <!-- Use the default slot to avoid extra spaces -->
                        <template #default>重置</template></a-button
                      >
                    </a-space>
                  </a-form-item>
                </a-col>
              </a-row>
            </a-form>
          </template>
        </TableHeader>
      </template>
      <template #table-config>
        <a-space :size="15">
          <a-button type="outline" status="warning" @click="exportLists" :loading="loading">
            <!-- Use the default slot to avoid extra spaces -->
            <template #default>导出</template>
          </a-button>
        </a-space>
        <a-space :size="15">
          <TableConfig
            @update-border="onUpdateBorder"
            @update-striped="onUpdateStriped"
            @refresh="doRefresh"
          />
        </a-space>
      </template>
      <template #default>
        <a-table
          :stripe="(striped as boolean)"
          :loading="tableLoading"
          :data="dataList"
          :pagination="false"
          :column-resizable="true"
          :rowKey="rowKey"
          :columns="tableColumns.list"
          table-layout-fixed
          :bordered="{ wrapper: true, cell: bordered }"
          :scroll="{ x: 1500 }"
          :scrollbar="{ type: 'track', outerClass: '', outerStyle: '' }"
        >
          <template #columns>
            <a-table-column
              v-for="item of tableColumns.list"
              :key="item.key"
              align="left"
              :width="item.width"
              :ellipsis="true"
              :tooltip="true"
              :title="(item.title as string)"
              :data-index="(item.key as string)"
              :fixed="item.fixed"
            >
              <template v-if="item.key === 'index'" #cell="{ rowIndex }">
                {{ rowIndex + 1 }}
              </template>
              <template v-else-if="item.key === 'add'" #cell="{ record }">
                <a-tag color="blue" v-if="record.add === true">增加</a-tag>
                <a-tag color="red" v-else-if="record.add === false">减少</a-tag>
              </template>
              <template v-else-if="item.key === 'remarks'" #cell="{ record }">
                <span v-if="record.remarks">{{ record.remarks }}</span>
                <span v-else>--- </span>
              </template>
              <template v-else-if="item.key === 'type'" #cell="{ record }">
                <span
                  v-for="(items, index) in typeLists"
                  :key="index"
                  v-show="items.type == record.type"
                  >{{ items.message }}</span
                >
              </template>
            </a-table-column>
          </template>
        </a-table>
      </template>
      <template #footer>
        <TableFooter ref="tableFooterRef" :pagination="pagination" />
      </template>
    </TableBody>
  </div>
</template>

<script lang="ts">
  import * as http from '@/api/http'
  import { getBillType } from '@/api/url'
  import { getUserbill, exportUserbill } from '@/api/user'
  import useCommonStore from '@/store/modules/common'
  import { usePagination, useRowKey, useTable, useTableHeight } from '@/hooks/table'
  import { defineComponent, getCurrentInstance, onMounted, reactive, ref } from 'vue'
  export default defineComponent({
    name: 'UserList',
    setup() {
      const form = reactive({
        remarks: '',
        userId: '',
        type: '',
        addTime: '',
        add: '',
        endTime: '',
        addBalance: '',
        endBalance: '',
      })
      const userCommon = useCommonStore()
      const typeLists: any = ref([])
      const table = useTable()
      const rowKey = useRowKey('id')
      const pagination = usePagination(doRefresh)
      const userLists: any = ref([])
      const tableColumns: any = reactive({
        list: [
          {
            title: '交易前余额',
            key: 'addBalance',
            dataIndex: 'addBalance',
            checked: true,
            width: 240,
          },
          {
            title: '交易余额',
            key: 'balance',
            dataIndex: 'balance',
            checked: true,
            width: 225,
          },
          {
            title: '交易后余额',
            key: 'endBalance',
            dataIndex: 'endBalance',
            checked: true,
            width: 225,
          },
          {
            title: '类型',
            key: 'type',
            dataIndex: 'type',
            checked: true,
            width: 225,
          },
          {
            title: '状态',
            key: 'add',
            dataIndex: 'add',
            checked: true,
            width: 225,
          },
          {
            title: '备注',
            key: 'remarks',
            dataIndex: 'remarks',
            checked: true,
            width: 225,
          },
          {
            title: '创建时间',
            key: 'createTime',
            dataIndex: 'createTime',
            checked: true,
            width: 190,
          },
        ],
      })
      const expandAllFlag = ref(true)
      const tableLoading = ref(true)
      const btnLoading = ref(false)
      const visible = ref(false)
      const loading = ref(false)
      const exportLists = () => {
        loading.value = true
        userCommon.downLoads(exportUserbill, form, '客户消费列表').then(() => {
          loading.value = false
        })
      }
      function getTypeList() {
        http
          .get({
            url: getBillType,
            data: () => {
              return {}
            },
          })
          .then((res) => {
            typeLists.value = res.data
          })
      }
      function doRefresh(value?: number) {
        // eslint-disable-next-line prettier/prettier
        tableLoading.value = true
        if (value) {
          pagination.page = value
        }
        http
          .get({
            url: getUserbill,
            data: () => {
              return {
                page: pagination.page,
                size: pagination.pageSize,
                ...form,
              }
            },
          })
          .then((res) => {
            tableLoading.value = false
            table.handleSuccess(res.data.list)
            pagination.setTotalSize((res.data as any).totalSize)
          })
          .catch(console.log)
      }

      function onUpdateBorder(isBordered: boolean) {
        table.bordered.value = isBordered
      }
      function onUpdateStriped(isStriped: boolean) {
        table.striped.value = isStriped
      }
      const serchForm = ref()
      const resetSeach = () => {
        serchForm.value?.resetFields()
        form.addBalance = ''
        form.endBalance = ''
        doRefresh(1)
      }
      onMounted(async () => {
        table.tableHeight.value = await useTableHeight(getCurrentInstance())
        doRefresh()
        getTypeList()
      })
      return {
        visible,
        btnLoading,
        userLists,
        doRefresh,
        serchForm,
        resetSeach,
        tableLoading,
        form,
        loading,
        ...table,
        rowKey,
        expandAllFlag,
        tableColumns,
        pagination,
        getTypeList,
        typeLists,
        exportLists,
        onUpdateBorder,
        onUpdateStriped,
      }
    },
  })
</script>
<style lang="less" scoped>
  :deep(.arco-modal) {
    .arco-modal-footer {
      text-align: right !important;
    }
  }
  :deep(.arco-modal-header .arco-modal-title) {
    display: flex;
    justify-content: flex-start !important;
  }
</style>
